package com.prod.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.prod.common.utils.DateUtils;
import com.prod.system.domain.TShop;
import com.prod.system.domain.TShopImages;
import com.prod.system.mapper.TShopImagesMapper;
import com.prod.system.mapper.TShopMapper;
import com.prod.system.service.ITShopImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 店铺图片列表Service业务层处理
 *
 * @author zhw
 * @date 2024-05-09
 */
@Service
public class TShopImagesServiceImpl implements ITShopImagesService {
    @Autowired
    private TShopImagesMapper tShopImagesMapper;
    @Autowired
    private TShopMapper tShopMapper;
    /**
     * 查询店铺图片列表
     *
     * @param id 店铺图片列表主键
     * @return 店铺图片列表
     */
    @Override
    public TShopImages selectTShopImagesById(Long id) {
        return tShopImagesMapper.selectTShopImagesById(id);
    }

    /**
     * 查询店铺图片列表列表
     *
     * @param tShopImages 店铺图片列表
     * @return 店铺图片列表
     */
    @Override
    public List<TShopImages> selectTShopImagesList(TShopImages tShopImages) {
        List<TShopImages> shopImagesList = tShopImagesMapper.selectTShopImagesList(tShopImages);
        if(!CollectionUtil.isEmpty(shopImagesList)){
            List<Long> shopIdList = shopImagesList.stream().map(TShopImages::getShopId).distinct().collect(Collectors.toList());
            List<TShop> shopList = tShopMapper.selectByIds(shopIdList);
            if(!CollectionUtil.isEmpty(shopList)){
                Map<Long, TShop> shopMap = shopList.stream().collect(Collectors.toMap(TShop::getId, Function.identity()));
                for (TShopImages shopImages : shopImagesList) {
                    shopImages.setShopName(shopMap.get(shopImages.getShopId()));
                }
            }


        }
        return shopImagesList;
    }

    /**
     * 新增店铺图片列表
     *
     * @param tShopImages 店铺图片列表
     * @return 结果
     */
    @Override
    public int insertTShopImages(TShopImages tShopImages) {
        String oldUrl = tShopImages.getOldUrl();
        List<String> imageList = Arrays.stream(oldUrl.split(",")).collect(Collectors.toList());
        for (String iamgeUrl : imageList) {
            tShopImages.setOldUrl(iamgeUrl);
            tShopImages.setCreateTime(DateUtils.getNowDate());
            tShopImagesMapper.insertTShopImages(tShopImages);
        }
        return 1;
    }

    /**
     * 修改店铺图片列表
     *
     * @param tShopImages 店铺图片列表
     * @return 结果
     */
    @Override
    public int updateTShopImages(TShopImages tShopImages) {
        tShopImages.setUpdateTime(DateUtils.getNowDate());
        return tShopImagesMapper.updateTShopImages(tShopImages);
    }

    /**
     * 批量删除店铺图片列表
     *
     * @param ids 需要删除的店铺图片列表主键
     * @return 结果
     */
    @Override
    public int deleteTShopImagesByIds(Long[] ids) {
        return tShopImagesMapper.deleteTShopImagesByIds(ids);
    }

    /**
     * 删除店铺图片列表信息
     *
     * @param id 店铺图片列表主键
     * @return 结果
     */
    @Override
    public int deleteTShopImagesById(Long id) {
        return tShopImagesMapper.deleteTShopImagesById(id);
    }

    @Override
    public List<String> downloadShopImages(TShopImages tShopImages) {
        List<TShopImages> shopImagesList = tShopImagesMapper.selectTShopImagesList(tShopImages);
        if(CollectionUtil.isEmpty(shopImagesList)){
            return null;
        }
        return shopImagesList.stream().map(x -> "" + x.getOldUrl()).collect(Collectors.toList());
    }
}
